﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CuongHanh.Context;
using CuongHanh.DAL;
using CuongHanh.Models;
using CuongHanh.Security;
using Ext.Net.MVC;

namespace CuongHanh.Controllers.BO
{
    [CustomAuthorize(Key = "Management", Description = "Manage all apps")]
    public class ACollectionController : ABaseController
    {
        public ActionResult Index(int branchId)
        {
            ViewBag.branchId = branchId;
            return View();
        }

        public ActionResult ShowAdd(string containerId,
                                    int branchId)
        {
            return ShowWindow("Add",
                              containerId,
                              new Collection());
        }

        [HttpPost]
        public ActionResult Add(Collection model)
        {
            Collection newModel = new Collection(model);
            UpdateResult result = InsertCollection(newModel);
            ShowWindow("wdModify",
                       false);
            ReloadStore("storeGvData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult Command(string containerId,
                                    string command,
                                    int id)
        {
            if (command == "Delete")
            {
                UpdateResult result = DeleteCollection(id);
                ReloadStore("storeGvData");
                ShowNotification(result);
            }
            if (command == "Edit")
            {
                Collection model = GetCollection(id);
                return ShowWindow("Edit",
                                  containerId,
                                  model);
            }
            return this.Direct();
        }

        [HttpPost]
        public ActionResult Edit(Collection model)
        {
            UpdateResult result = UpdateCollection(model);
            ShowWindow("wdModify",
                       false);
            ReloadStore("storeGvData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult GetDatas(int branchId,
                                     int start,
                                     int limit,
                                     string sort,
                                     string query)
        {
            return Content(GetCollectionsByBranch(branchId,
                                                  start,
                                                  limit,
                                                  sort,
                                                  query),
                           "json");
        }

        #region Collection Context

        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        public string GetCollectionsByBranch(int branchId,
                                             int start,
                                             int limit,
                                             string sort,
                                             string q)
        {
            var query = _unitOfWork.Where<Collection>(c => (c.NameVi.Contains(q) || c.NameEn.Contains(q) ) && !c.Inactive)
                    .Select(c => new
                    {
                        c.Id,
                        c.NameVi,
                        c.NameEn,
                        c.ShowIndex,
                        c.Invisible,
                        c.LastModified
                    });

            return FormatResult(query.Count(),
                                query.OrderBy(c => c.NameVi).Skip(start).Take(limit));
        }

        public Collection GetCollection(int id)
        {
            return _unitOfWork.GetById<Collection>(id);
        }

        public UpdateResult InsertCollection(Collection model)
        {
            _unitOfWork.Insert(model);
            _unitOfWork.Save();
            return new UpdateResult(1,
                                    string.Format("Insert Collection {0} successful!",
                                                  model.NameVi));
        }

        public UpdateResult UpdateCollection(Collection newModel)
        {
            Collection model = _unitOfWork.GetById<Collection>(newModel.Id);
            if (model != null)
            {
                model.UpdateInfoFrom(newModel);
                _unitOfWork.Save();
                return new UpdateResult(1,
                                        string.Format("Update Collection {0} successful!",
                                                      newModel.NameVi));
            }
            return new UpdateResult(4,
                                    string.Format("Update Collection {0} error!",
                                                  newModel.NameVi));
        }

        public UpdateResult DeleteCollection(int id)
        {
            Collection oldModel = _unitOfWork.GetById<Collection>(id);
            if (oldModel != null)
            {
                oldModel.Delete();
                _unitOfWork.Save();
                return new UpdateResult(1,
                                        string.Format("Delete Collection {0} successful!",
                                                      id));
            }
            return new UpdateResult(4,
                                    string.Format("Delete Collection {0} error!",
                                                  id));
        }

        public string GetCollectionsComboByBranch(int branchId)
        {

            var query = _unitOfWork.Where<Collection>(c => !c.Inactive)
                    .Select(c => new
                    {
                        c.Id,
                        Name = c.NameVi,
                    }).OrderBy(c => c.Name).ToList();

            return FormatResult(query.Count,
                                query);

        }

        #endregion

    }
}
